import 'package:flutter/cupertino.dart';
import 'package:flutter_pleasure_video/constant/app_icon.dart';
import 'package:flutter_pleasure_video/constant/color.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/src/simple/get_state.dart';


class AppIconButton extends StatelessWidget {
  AppIconButton({
    Key? key,
    this.icon,
    this.svgName,
    required this.iconSize,
    this.buttonHeight,
    this.buttonWidth,
    this.iconColor,
    this.bgColor = AppColor.transparent,
    this.isCircle = false,
    this.onTap,
  })  : assert(icon != null || svgName != null),
        super(key: key);

  final IconData? icon;
  final String? svgName;
  final Color? iconColor;
  final Color bgColor;
  final Function()? onTap;
  final double iconSize;
  final double? buttonHeight;
  final double? buttonWidth;
  final bool isCircle;

  @override
  Widget build(BuildContext context) {
    IsPressedLogic pressedLogic = IsPressedLogic();
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      child: Obx(() => pressedLogic.isPressed.value
          ? Opacity(
              opacity: 0.5,
              child: icon != null ? _buildIcon() : _buildAppIcon(),
            )
          : icon != null
              ? _buildIcon()
              : _buildAppIcon()),
      onTapDown: (details) {
        pressedLogic.setPressed(true);
      },
      onTapUp: (details) {
        pressedLogic.setPressed(false);
      },
      onTapCancel: () {
        pressedLogic.setPressed(false);
      },
      onTap: onTap,
    );
  }

  Widget _buildIcon() {
    return Container(
      width: buttonWidth == null ? iconSize : buttonWidth,
      height: buttonHeight == null ? iconSize : buttonHeight,
      color: isCircle ? null : bgColor,
      decoration: isCircle
          ? BoxDecoration(
              color: bgColor,
              shape: BoxShape.circle,
            )
          : null,
      alignment: Alignment.center,
      child: Icon(
        icon,
        color: iconColor,
        size: iconSize,
      ),
    );
  }

  Widget _buildAppIcon() {
    return AppIcon.getAppIcon(
      svgName,
      iconSize,
      containerHeight: buttonHeight == null ? iconSize : buttonHeight,
      containerWidth: buttonWidth == null ? iconSize : buttonWidth,
      color: iconColor,
      bgColor: bgColor,
      isCircle: isCircle,
    );
  }


}

class IsPressedLogic extends GetxController {
  RxBool isPressed = false.obs;

  void setPressed(bool isPressed) => this.isPressed.value = isPressed;
}
